package com.example.malltest0.controller;


import com.example.malltest0.common.api.CommonResult;
import com.example.malltest0.mbg.model.Blogs;
import com.example.malltest0.mbg.model.Followers;
import com.example.malltest0.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/blog")
@Api(tags = "BlogController", description = "博客管理")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加博客")
    public CommonResult insertBlog(@RequestBody Blogs blogs, @RequestParam Long fid){
        blogService.insertBlog(blogs,fid);
        return CommonResult.success("添加博客成功");
    }

    @ResponseBody
    @RequestMapping(value = "/selectOne",method = RequestMethod.GET)
    @ApiOperation("根据id查询")
    public CommonResult<Blogs> selectOne(@RequestParam Long bid){
        Blogs blogs=blogService.selectOne(bid);
        return CommonResult.success(blogs);
    }

    @ResponseBody
    @RequestMapping(value = "/selectByFid",method = RequestMethod.GET)
    @ApiOperation("根据fid查询")
    public CommonResult< PageInfo<Followers>> selectByFid(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,@RequestParam Long fid){
        PageHelper.startPage(pageNum, pageSize);
        List<Followers> list=blogService.selectByFid(fid);
        if(list!=null){
            //通过构造PageInfo对象获取分页信息，如当前页码，总页数，总条数
            PageInfo<Followers> pageInfo = new PageInfo<Followers>(list);
            return CommonResult.success(pageInfo);
        }
        return CommonResult.failed();
    }
    @ResponseBody
    @RequestMapping(value = "/getByFid",method = RequestMethod.GET)
    @ApiOperation("获取博客")
    public CommonResult<PageInfo<Blogs>>getBlogByFid(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,@RequestParam Long fid){

        PageHelper.startPage(pageNum, pageSize);
        List<Blogs>list=blogService.getBlogByFid(fid);
        if(list!=null){
            //通过构造PageInfo对象获取分页信息，如当前页码，总页数，总条数
            PageInfo<Blogs> pageInfo = new PageInfo<Blogs>(list);
            return CommonResult.success(pageInfo);
        }
        return CommonResult.failed();
    }
}
